import { OrderSortType, CommonOrderStatus } from '../common/docDefinition';
import { Engine } from '../engine';

// 往来过滤器
export class DealFilter {
    /**
     * 单号
     * @type {string|null}
     */
    id = null;

    /**
     * 单据标题
     * @type {string|null}
     */
    title = null;

    /**
     * 状态 0 all, 1 未审核，2 已审核 for desktop and web 注意取的code
     * @type {CommonOrderStatus|null}
     */
    status = null;

    /**
     * 仓库
     * @type {WareHouse|null}
     */
    wareHouse = null;

        /**
     * 往来单位Id
     * @type {string|null}
     */
    targetObjId = null;

    /**
     * 往来单位名称
     * @type {string|null}
     */
    targetObjName = null;

    /**
     * 规格商品名称
     * @type {string|null}
     */
    subCommodityName = null;

    /**
     * 开始时间
     * @type {Date|null}
     */
    startDate = null;

    /**
     * 结束时间
     * @type {Date|null}
     */
    endDate = null;

    /**
     * 创建开始时间
     * @type {Date|null}
     */
    createStartDate = null;

    /**
     * 创建结束时间
     * @type {Date|null}
     */
    createEndDate = null;

    /**
     * 门店过滤
     * @type {string|null}
     */
    storeId = null;

    /**
     * 排序方式,默认按创建时间排序
     * @type {OrderSortType}
     */
    sortType = OrderSortType.CreateTime.code;

    /**
     * 构造函数
     * @param {string|null} storeId 
     */
    constructor(storeId = null) {
        this.storeId = storeId || Engine.profile?.filterStore?.id;
    }

    /**
     * 清空过滤器
     */
    clear() {
        this.id = null;
        this.title = null;
        this.wareHouse = null;
        this.targetObjName = null;
        this.targetObjId = null;
        this.subCommodityName = null;
        this.startDate = null;
        this.endDate = null;
        this.status = null;
    }

    /**
     * 克隆过滤器
     * @param {DealFilter} filter 
     */
    static clone(filter) {
        let newFilter = new DealFilter();
        newFilter.id = filter.id;
        newFilter.title = filter.title;
        newFilter.wareHouse = filter.wareHouse;
        newFilter.targetObjName = filter.targetObjName;
        newFilter.targetObjId = filter.targetObjId;
        newFilter.subCommodityName = filter.subCommodityName;
        newFilter.startDate = filter.startDate;
        newFilter.endDate = filter.endDate;
        newFilter.status = filter.status;
        newFilter.sortType = filter.sortType;
        newFilter.storeId = filter.storeId;
        return newFilter;
    }

    /**
     * 转换为JSON对象
     * @param {boolean} withState 
     * @returns {Object}
     */
    toJson(withState = false) {
        let ret = {};

        if (this.status != null && this.status === CommonOrderStatus.WaitReview.code && withState) {
            ret['a_orderStatus'] = 0;
        }
        if (this.status != null && this.status === CommonOrderStatus.Passed.code && withState) {
            ret['a_orderStatus'] = 1;
        }

        if (this.id && this.id.length > 0) ret["a_lke_id"] = this.id;
        if (this.title && this.title.length > 0) ret["a_lke_orderTitle"] = this.title;
        if (this.wareHouse) ret["b_storageId"] = this.wareHouse.id;
        if (this.targetObjName && this.targetObjName.length > 0) {
            ret["objName"] = this.targetObjName;
        }
        if (this.targetObjId) ret["objId"] = this.targetObjId;

        if (this.subCommodityName && this.subCommodityName.length > 0) {
            ret["specName"] = this.subCommodityName;
        }

        // 记录时间过滤
        if (this.createStartDate && this.createEndDate && this.createEndDate > this.createStartDate) {
            ret["a_gte_createTime"] = this.formatDate(this.createStartDate);
            ret["a_lte_createTime"] = this.formatDate(this.createEndDate);
        } else if (this.createStartDate && !this.createEndDate) {
            ret["a_gte_createTime"] = this.formatDate(this.createStartDate);
        } else if (this.createEndDate && !this.createStartDate) {
            ret["a_lte_createTime"] = this.formatDate(this.createEndDate);
        }

        // 业务时间过滤
        if (this.startDate && this.endDate && this.endDate > this.startDate) {
            ret["a_gte_businessTime"] = this.formatDate(this.startDate);
            ret["a_lte_businessTime"] = this.formatDate(this.endDate);
        } else if (this.startDate && !this.endDate) {
            ret["a_gte_businessTime"] = this.formatDate(this.startDate);
        } else if (this.endDate && !this.startDate) {
            ret["a_lte_businessTime"] = this.formatDate(this.endDate);
        }

        if (this.sortType === OrderSortType.CreateTime.code) {
            ret["sort"] = "a.create_time desc";
        } else if (this.sortType === OrderSortType.OperationTime.code) {
            ret["sort"] = "a.business_time desc, a.create_time desc";
        }

        // 门店过滤
        if (this.storeId) {
            ret["storeId"] = this.storeId;
        }

        return ret;
    }

    /**
     * 格式化日期
     * @param {Date} date 
     * @returns {string}
     */
    formatDate(date) {
        const year = date.getFullYear();
        const month = String(date.getMonth() + 1).padStart(2, '0');
        const day = String(date.getDate()).padStart(2, '0');
        const hours = String(date.getHours()).padStart(2, '0');
        const minutes = String(date.getMinutes()).padStart(2, '0');
        const seconds = String(date.getSeconds()).padStart(2, '0');
        return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    }
}